package ui.control
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import loaderData.LoaderSys;
	
	import texture.BitmapEx;
	
	import ui.GuiEnum;
	import ui.GuiWnd;

	public class GuiScrollBar extends GuiWnd
	{
		private var upButton:GuiButton;
		private var downButton:GuiButton;
		
		
		private var backSprite:Sprite;
		private var dragButton:Sprite;
		
		private var backSpriteBitmap:BitmapEx;
		private var dragButtonBitmap:BitmapEx;

		private var dragButtonHeight:int; //拖动滑块的高度
		
		private var dragRect:Rectangle = new Rectangle();
		
		private var wheel:Number;
		
		private var bdown:Boolean = false;
		
		public function GuiScrollBar()
		{

			backSprite = new Sprite();
			dragButton = new Sprite();
			
			addChild(backSprite);
			addChild(dragButton);
			
			wheel = 0.15;
			nWidth = 12;
			nHeight = 50;
			nDragHeight = 10;

			this.addEventListener(Event.ADDED_TO_STAGE,onEvent);
			
			progressValue = 0;
		}
		
		override public function init(xml:XML):void
		{
			if(xml.@x)
			{
				this.x = xml.@x;
			}
			
			if(xml.@y)
			{
				this.y = xml.@y;
			}
			
			if(xml.@widht)
			{
				nWidth = xml.@widht;
			}
			if(xml.@height)
			{
				nHeight = xml.@height;
			}
			
			if(xml.@drag_heidht)
			{
				nDragHeight = xml.@drag_heidht;
			}
			
			if(xml.@wheel)
			{
				wheel = xml.@wheel;
			}
			
			if(xml.@value)
			{
				progressValue = xml.@value;
			}
			
			if(xml.@back_img)
			{
				var bit:BitmapEx = LoaderSys.inst.getBitmapExByClass(xml.@back_img);
				backBitmap = bit;
			}
			if(xml.@btn_img)
			{
				var btnbit:BitmapEx = LoaderSys.inst.getBitmapExByClass(xml.@btn_img);
				btnBitmap = btnbit;
			}
		}

		protected function onEvent(event:Event):void
		{
			if(event.target == this)
			{
				dragButton.addEventListener(MouseEvent.MOUSE_DOWN, onBtndown);
				this.stage.addEventListener(MouseEvent.MOUSE_UP, onBtnup);
				this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onBtnMouse);
				this.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
			}
		}
		
		protected function onWheel(event:MouseEvent):void
		{
			if(event.delta > 0)
			{
				up();
			}
			else
			{
				down();
			}
		}
		
		protected function onBtnMouse(event:MouseEvent):void
		{
			if(bdown)
			{
				sendupdataEvent();
			}
		}
		
		public var oldProgressNum:Number = 0;
		private function sendupdataEvent():void
		{
			if(progressValue == oldProgressNum) return;
			
			this.dispatchEvent(new Event(GuiEnum.SCROLLBAR_UPDATA));
			
			oldProgressNum = progressValue;
		}
		
		protected function onBtnup(event:MouseEvent):void
		{
			bdown = false;
			dragButton.stopDrag();
			event.stopPropagation();
		}
		
		protected function onBtndown(event:MouseEvent):void
		{
			bdown = true;
			dragButton.startDrag(false,dragRect);
			
			event.stopPropagation();
		}
		
		private function up():void
		{
			var num:Number = getProgress();
			
			if(num <= 0)
				return;
			
			num -= wheel;
			
			progressValue = num;
			
			sendupdataEvent();
		}
		
		private function down():void
		{
			var num:Number = getProgress();
			
			if(num >= 1)
				return;
			
			num += wheel;
			
			progressValue = num;
			
			sendupdataEvent();
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			if(child && child is GuiButton)
			{
				pushBtn(child as GuiButton);
			}
			return super.addChild(child);
		}
		
		public function pushBtn(btn:GuiButton):void
		{
			if(!upButton)
			{
				upButton = btn;
				upButton.addEventListener(MouseEvent.CLICK, onBtnupClick);
			}
			else if(!downButton)
			{
				downButton = btn;
				downButton.addEventListener(MouseEvent.CLICK, onBtndownClick);
			}
			
			updataBtnPos();
		}
		
		protected function onBtnupClick(event:MouseEvent):void
		{
			up();
		}
		
		protected function onBtndownClick(event:MouseEvent):void
		{
			down();
		}

		/**
		 * 计算上下两按扭的坐标(只计算Y值。X在外面附值) 
		 * 
		 */		
		private function updataBtnPos():void
		{
			if(!upButton || downButton)
				return;
			
			upButton.y = 0 - upButton.height;
			
			downButton.y = nHeight+ downButton.height;
		}
		
		private function updataBtnHeight():void
		{
			dragButton.graphics.clear();

			if(btnBitmap)
			{
				btnBitmap.height = nDragHeight;
			}
			else
			{
				dragButton.graphics.beginFill(0xcccccc);
				dragButton.graphics.drawRect(0, 0, nWidth, nDragHeight);
				dragButton.graphics.endFill();
			}
			
			updataDrag();
		}
		
		private function updataBackSize():void
		{
			backSprite.graphics.clear();
			
			if(backBitmap)
			{
				backBitmap.width = nWidth;
				backBitmap.height = nHeight;
			}
			else
			{
				backSprite.graphics.beginFill(0x0);
				backSprite.graphics.drawRect(0, 0, nWidth, nHeight);
				backSprite.graphics.endFill();
			}
			
			updataBtnPos();
			updataDrag();
		}
		
		private function updataDrag():void
		{
			dragRect.width = 0;
			dragRect.height = nWidth - nDragHeight;
		}
		

		public function get backBitmap():BitmapEx
		{
			return backSpriteBitmap;
		}

		public function set backBitmap(value:BitmapEx):void
		{
			backSpriteBitmap = value;
			
			clearBackImg();
			
			if(backSpriteBitmap)
				backSprite.addChild(backSpriteBitmap);
			
			updataBackSize();
			
		}

		public function get btnBitmap():BitmapEx
		{
			return dragButtonBitmap;
		}

		public function set btnBitmap(value:BitmapEx):void
		{
			dragButtonBitmap = value;
			
			clearBtnImg();
			
			if(dragButtonBitmap)
				dragButton.addChild(dragButtonBitmap);
			
			updataBtnHeight();
		}

		override public function set nWidth(value:int):void
		{
			super.nWidth = value;
			updataBackSize();
		}

		override public function set nHeight(value:int):void
		{
			super.nHeight = value;
			updataBackSize();
		}
		
		
		public function get nDragHeight():int
		{
			return dragButtonHeight;
		}

		public function set nDragHeight(value:int):void
		{
			dragButtonHeight = value;
			updataBtnHeight();
		}

		
		public function set progressValue(value:Number):void
		{
			value = value>1 ? 1 : value;
			value = value<0 ? 0 : value;
			
			dragButton.y = int(value * (nHeight - nDragHeight));
		}
		
		public function get progressValue():Number
		{
			return getProgress();
		}
		
		private function getProgress():Number
		{
			var i:Number = dragButton.y /(nHeight - nDragHeight);
			return i;
		}

		/**
		 * 清除背景的所有的子对像 
		 * 
		 */		
		public function clearBackImg():void
		{
			while(backSprite.numChildren > 0)
				backSprite.removeChildAt(0);
		}
		
		/**
		 * 清除滑块中的所有的子对像 
		 * 
		 */		
		public function clearBtnImg():void
		{
			while(dragButton.numChildren > 0)
				dragButton.removeChildAt(0);
		}
		
		/**
		 * 显示或隐藏滑快按扭 
		 * 
		 */		
		public function ShowDragButton(bShow:Boolean = true):void
		{
			dragButton.visible = bShow;
		}

	}
}